// 引入 express 
var express = require('express');
const { query } = require('../utils/db');
// 引入 路由
var myRouter = express.Router();
//引入db
var db = require('../utils/db');

// 测试接口

// myRouter.get('/olderly',function(req,res){
//     console.log('获取',req);
//     console.log('获取',req.query);
//     var sql='SELECT * FROM olderly_health,olderly_infor ';
//     db.query(sql,function(err,data){
//         console.log(err,data);
//         if(!err){
//             if(data.lengtn){
//                 res.json({error:0,msg:'获取健康信息成功',data:data})
//             }else{
//                 res.json({error:1,msg:'获取健康信息失败'})
//             }
//         }
//     })
// })

//查询
myRouter.route('/olderly/health').get(function (request, response) {
    //request 前端给后台发送的东西
    //response 后台给前端返回的东西
    // console.log('获取', request.query);
    let sql = 'SELECT * FROM olderly_health JOIN olderly_infor ON olderly_health.olderly_id = olderly_infor.olderly_id LIMIT ' + (request.query.page - 1) * request.query.pageSize + ' , ' + request.query.pageSize;
    db.query(sql, function (err, data) {
        console.log(err, data);
        if (!err) {
            if (data.length) {
                response.json({ error: 0, msg: '获取健康信息成功', data: data })
            } else {
                response.json({ error: 1, msg: '获取健康信息失败' })
            }
        }
    })
})

// 筛选
myRouter.route('/search/health').get(function (req, res) {
    // console.log("获取", req.query);
    let sql = 'SELECT * FROM olderly_health JOIN olderly_infor ON olderly_health.olderly_id = olderly_infor.olderly_id WHERE olderly_infor.userName LIKE "%' + req.query.userName + '%" AND olderly_health.health_id LIKE "%' + req.query.health_id + '%"  AND olderly_health.modifyTime BETWEEN "' + req.query.TimeL + '" AND "' + req.query.TimeH + '" LIMIT ' + (req.query.page - 1) * req.query.pageSize + ' , ' + req.query.pageSize;
    db.query(sql, function (err, data) {
        if (!err) {
            if (data.length) {
                res.json({ errror: 0, msg: '搜索健康信息成功', data: data })
            } else {
                res.json({ error: 1, msg: '搜索信息失败' })
            }
        }
    })
})

// 条数
myRouter.route('/olderly/count').get(function (req, res) {
    let sql = 'SELECT count(*) as count FROM olderly_health JOIN olderly_infor ON olderly_health.olderly_id = olderly_infor.olderly_id '
    db.query(sql, function (err, data) {
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '获取健康信息条数成功', data: data })
            } else {
                res.json({ error: 1, msg: '获取健康信息条数失败' })
            }
        }
    })
})

// 修改
myRouter.route('/healthdata/edit').post(function (req, res) {
    var sql = 'update olderly_health set heartRate=' + req.body.heartRate + ', bloodPressure="' + req.body.bloodPressure0 + '/' + req.body.bloodPressure1 + '" ,bloodSugar=' + req.body.bloodSugar + ', bloodOxygen=' + req.body.bloodOxygen + ',bloodFat=' + req.body.bloodFat + ' , dietary=' + req.body.dietary + ' where health_id=' + req.body.health_id;
    db.query(sql, function (err, data) {
        console.log(sql);
        if (!err) {
            res.json({ error: 0, msg: '修改健康信息条数成功', data: data })
        } else {
            res.json({ error: 1, msg: '修改健康信息条数失败' })
        }
    })
})





// -------------------------------------------------------分值管理--------------------------------------------------------------------

// 查询
myRouter.route('/score/management').get(function (req, res) {
    // console.log(sql);
    // ORDER BY entry_name ,Score DESC 分组加排序
    let sql = 'SELECT * FROM Check_item_score  ORDER BY entry_name ,Score DESC LIMIT ' + (req.query.page - 1) * req.query.pageSize + ' , ' + req.query.pageSize;
    db.query(sql, function (err, data) {
        console.log("zhe:", req, "查询语句", sql);
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '获取分值信息成功', data: data })
            } else {
                res.json({ error: 1, msg: '获取分值信息失败' })
            }
        }
    })
})

// 条数
myRouter.route('/score/count').get(function (req, res) {
    let sql = 'SELECT count(*) as count FROM Check_item_score'
    db.query(sql, function (err, data) {
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '获取健康信息条数成功', data: data })
            } else {
                res.json({ error: 1, msg: '获取健康信息条数失败' })
            }
        }
    })
})

// 添加
myRouter.route('/score/add').post(function (req, res) {
    // console.log(req);
    // let sql=`INSERT INTO Check_item_score(Check_item_score_id,entry_name,Lower_limit_of_value,Upper_limit_of_value,Company,Score,performance,state) VALUES (NULL,${req.body.entry_name},${req.body.Lower_limit_of_value},${req.body.Upper_limit_of_value},${req.body.Company},${req.body.Score},${req.body.Performance},DEFAULT)`

    let sql = 'INSERT INTO Check_item_score(Check_item_score_id,entry_name,Lower_limit_of_value,Upper_limit_of_value,Company,Score,performance,State) VALUES (NULL,"' + req.body.entry_name + '","' + req.body.Lower_limit_of_value + '","' + req.body.Upper_limit_of_value + '","' + req.body.Company + '","' + req.body.Score + '","' + req.body.Performance + '","' + req.body.State + '")'
    db.query(sql, function (err, data) {
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '添加健康信息条数成功', data: data })
            } else {
                res.json({ error: 1, msg: '添加健康信息条数失败' })
            }
        }
    })
})


// 删除 /del/score
myRouter.route('/score/del').post(function (req, res) {
    // console.log(req);
    let sql = 'delete from Check_item_score where Check_item_score_id=' + req.body.Check_item_score_id;
    db.query(sql, function (err, data) {
        if (!err) {
            res.json({ error: 0, msg: '删除成功', data: data })
        } else {
            res.json({ error: 1, msg: '删除失败', })
        }
    })
})
// 修改
myRouter.route('/score/edit').post(function (req, res) {
    // console.log(req);
    var sql = 'update Check_item_score set entry_name="' + req.body.entry_name + '",Lower_limit_of_value="' + req.body.Lower_limit_of_value + '" , Upper_limit_of_value="' + req.body.Upper_limit_of_value + '",Company="' + req.body.Company + '",Score="' + req.body.Score + '",Performance="' + req.body.Performance + '",State="' + req.body.State + '" where Check_item_score_id=' + req.body.Check_item_score_id;
    db.query(sql, function (err, data) {
        if (!err) {
            res.json({ error: 0, msg: '修改健康信息条数成功', data: data })
        } else {
            res.json({ error: 1, msg: '修改健康信息条数失败' })
        }
    })
})

// 筛选
myRouter.route('/score/search').get(function (req, res) {
    let sql = 'SELECT * FROM Check_item_score WHERE Check_item_score.entry_name LIKE "%' + req.query.entry_name + '%" AND Check_item_score.state="' + req.query.state + '" LIMIT ' + (req.query.page - 1) * req.query.pageSize + ' , ' + req.query.pageSize;
    db.query(sql, function (err, data) {
        console.log(sql);
        // console.log("went",err);
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '筛选健康信息条数成功', data: data })
            } else {
                res.json({ error: 1, msg: '筛选健康信息条数失败', data: data })
            }
        }
    })
})








// -------------------------------------------------------------------------------------评述配置-------------------------------------------------------------------------------------------------------



// 查询Comment configuration
myRouter.route('/Comment/configuration').get(function (req, res) {
    // console.log(sql);
    // ORDER BY entry_name ,Score DESC 分组加排序
    let sql = 'SELECT * FROM Comment_configuration LIMIT ' + (req.query.page - 1) * req.query.pageSize + ' , ' + req.query.pageSize;
    db.query(sql, function (err, data) {
        // console.log("zhe:", req, "查询语句", sql);
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '获取分值信息成功', data: data })
            } else {
                res.json({ error: 1, msg: '获取分值信息失败' })
            }
        }
    })
})

// 条数
myRouter.route('/Comment/count').get(function (req, res) {
    let sql = 'SELECT count(*) as count FROM Comment_configuration'
    db.query(sql, function (err, data) {
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '获取健康信息条数成功', data: data })
            } else {
                res.json({ error: 1, msg: '获取健康信息条数失败' })
            }
        }
    })
})

// 添加
myRouter.route('/Comment/add').post(function (req, res) {
    let sql = 'INSERT INTO Comment_configuration(Comment_configuration_id,Health_value_Lower_limit_of_value,Health_value_Upper_limit_of_value,performance,report,state) VALUES (NULL,' + req.body.Lower_limit_of_value + ',' + req.body.Upper_limit_of_value + ',"' + req.body.Performance + '","' + req.body.report + '",' + req.body.State + ')'
    db.query(sql, function (err, data) {
        // console.log(sql);
        console.log(data);
        if (!err) {
            if (data.length) {
                console.log(data);
                res.json({ error: 0, msg: '添加评述报告成功', data: data })
            } else {
                res.json({ error: 1, msg: '添加评述报告失败' })
            }
        }
    })
})


// 删除 
myRouter.route('/Comment/del').post(function (req, res) {
    // console.log(req);
    let sql = 'delete from Comment_configuration where Comment_configuration_id=' + req.body.Comment_configuration_id;
    db.query(sql, function (err, data) {
        if (!err) {
            res.json({ error: 0, msg: '删除成功', data: data })
        } else {
            res.json({ error: 1, msg: '删除失败', })
        }
    })
})
// 修改
myRouter.route('/Comment/edit').post(function (req, res) {
    console.log(req);
    var sql = 'update Comment_configuration set Health_value_Lower_limit_of_value="' + req.body.Lower_limit_of_value + '" , Health_value_Upper_limit_of_value="' + req.body.Upper_limit_of_value + '",Performance="' + req.body.Performance + '", State="' + req.body.State + '" ,report="'+req.body.report+'" where Comment_configuration_id=' + req.body.Comment_configuration_id;
    db.query(sql, function (err, data) {
        if (!err) {
            res.json({ error: 0, msg: '修改健康信息条数成功', data: data })
        } else {
            res.json({ error: 1, msg: '修改健康信息条数失败' })
        }
    })
})






// ---------------------------------------------------------------------评价列表-------------------------------------------------------------------------------------------------------


// ----------------------------------------------------评价列表：

// 查询Comment configuration
myRouter.route('/Evaluation/list').get(function (req, res) {
    // console.log(sql);
    // ORDER BY entry_name ,Score DESC 分组加排序
    let sql = 'SELECT * FROM `order`  LEFT JOIN olderly_infor ON `order`.olderly_id = olderly_infor.olderly_id LEFT JOIN service_type ON `order`.service_id = service_type.service_id LEFT JOIN escortPeople ON `order`.escortPeople_id = escortPeople.id LEFT JOIN evaluation ON `order`.eva_id = evaluation.eva_id LIMIT ' + (req.query.page - 1) * req.query.pageSize + ' , ' + req.query.pageSize;
    db.query(sql, function (err, data) {
        // console.log("zhe:", req, "查询语句", sql);
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '获取分值信息成功', data: data })
            } else {
                res.json({ error: 1, msg: '获取分值信息失败' })
            }
        }
    })
})

// 条数
myRouter.route('/Evaluation/count').get(function (req, res) {
    let sql = 'SELECT count(*) as count FROM `order` LEFT JOIN olderly_infor ON `order`.olderly_id = olderly_infor.olderly_id LEFT JOIN service_type ON `order`.service_id = service_type.service_id LEFT JOIN escortPeople ON `order`.escortPeople_id = escortPeople.id LEFT JOIN evaluation ON `order`.eva_id = evaluation.eva_id'
    db.query(sql, function (err, data) {
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '获取健康信息条数成功', data: data })
            } else {
                res.json({ error: 1, msg: '获取健康信息条数失败' })
            }
        }
    })
})


// 删除 
myRouter.route('/Evaluation/del').post(function (req, res) {
    // console.log(req);
    let sql = 'delete from Comment_configuration where order_id=' + req.body.order_id;
    db.query(sql, function (err, data) {
        if (!err) {
            res.json({ error: 0, msg: '删除成功', data: data })
        } else {
            res.json({ error: 1, msg: '删除失败', })
        }
    })
})

// 筛选
myRouter.route('/Evaluation/search').get(function (req, res) {
    let sql = 'SELECT * FROM `order` LEFT JOIN olderly_infor ON `order`.olderly_id = olderly_infor.olderly_id LEFT JOIN service_type ON `order`.service_id = service_type.service_id LEFT JOIN escortPeople ON `order`.escortPeople_id = escortPeople.id LEFT JOIN evaluation ON `order`.eva_id = evaluation.eva_id  WHERE order.orderNum LIKE "%' + req.query.orderNum + '%" AND escortPeople.name LIKE "%' + req.query.name + '%" AND olderly_infor.userName LIKE "%' + req.query.userName + '%"  AND evaluation.eva_time BETWEEN "' + req.query.TimeL + '" AND "' + req.query.TimeH + '"  LIMIT ' + (req.query.page - 1) * req.query.pageSize + ' , ' + req.query.pageSize;
    db.query(sql, function (err, data) {
        console.log(sql);
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '筛选健康信息条数成功', data: data })
            } else {
                res.json({ error: 1, msg: '筛选健康信息条数失败', data: data })
            }
        }
    })
})

// -----------------------------------------------------------------------评价配置

// 查询Comment configuration
myRouter.route('/configuration/Evaluation').get(function (req, res) {
    // console.log(sql);
    // ORDER BY entry_name ,Score DESC 分组加排序
    let sql = 'SELECT * FROM Evaluation_configuration  LIMIT ' + (req.query.page - 1) * req.query.pageSize + ' , ' + req.query.pageSize;
    db.query(sql, function (err, data) {
        // console.log("zhe:", req, "查询语句", sql);
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '获取评价配置成功', data: data })
            } else {
                res.json({ error: 1, msg: '获取评价配置失败' })
            }
        }
    })
})

// 条数
myRouter.route('/configuration/count').get(function (req, res) {
    let sql = 'SELECT count(*) as count FROM Evaluation_configuration'
    db.query(sql, function (err, data) {
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '获取评价配置条数成功', data: data })
            } else {
                res.json({ error: 1, msg: '获取评价配置条数失败' })
            }
        }
    })
})


// 添加
myRouter.route('/EvaluationConfiguration/add').post(function (req, res) {
    let sql = 'INSERT INTO Evaluation_configuration(Question_id,Question_name,answer1,answer2,creatTime,State) VALUES (NULL,"' + req.body.Question_name + '","' + req.body.answer1 + '","' + req.body.answer2 + '","' + req.body.creatTime + '",' + req.body.State + ')'
    db.query(sql, function (err, data) {
        console.log(sql);
        if (!err) {
            if (data.length) {
                console.log(data);
                res.json({ error: 0, msg: '添加评述配置成功', data: data })
            } else {
                res.json({ error: 1, msg: '添加评述配置失败' })
            }
        }
    })
})


// 修改
myRouter.route('/configuration/edit').post(function (req, res) {
    
    var sql = 'update Evaluation_configuration set Question_name="' + req.body.Question_name + '" , answer1="' + req.body.answer1 + '",answer2="' + req.body.answer2 + '", state=' + req.body.State+' WHERE Question_id=' + req.body.Question_id ;
    db.query(sql, function (err, data) {
        
        if (!err) {
            res.json({ error: 0, msg: '修改健康信息条数成功', data: data })
        } else {
            res.json({ error: 1, msg: '修改健康信息条数失败' })
        }
    })
})


// 删除 
myRouter.route('/configuration/del').post(function (req, res) {
    // console.log(req);
    let sql = 'delete from Evaluation_configuration where Question_id=' + req.body.Question_id;
    db.query(sql, function (err, data) {
        if (!err) {
            res.json({ error: 0, msg: '删除成功', data: data })
        } else {
            res.json({ error: 1, msg: '删除失败', })
        }
    })
})

// 筛选
myRouter.route('/configurationEvaluation/search').get(function (req, res) {
    let sql = 'SELECT * FROM Evaluation_configuration WHERE Evaluation_configuration.Question_name LIKE "%' + req.query.Question_name + '%"  AND Evaluation_configuration.State= ' + req.query.state + '  LIMIT ' + (req.query.page - 1) * req.query.pageSize + ' , ' + req.query.pageSize;
    db.query(sql, function (err, data) {
        console.log("查询",sql);
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '筛选健康信息条数成功', data: data })
            } else {
                res.json({ error: 1, msg: '筛选健康信息条数失败', data: data })
            }
        }
    })
})





// ------------------------------------------------------------------------------------小程序----------------------------------------------------------------------------
myRouter.route('/login/older').post(function (req, res) {
    console.log('older小程序登录了');
    let sql = 'select * from olderly_infor where userPassword="' + req.body.IDvalue + '" and phone="' + req.body.TELvalue + '"';
    db.query(sql, function (err, data) {
        if (!err) {
            if (data.length) {
                res.json({ error: 0, msg: '登录成功', data: data })
            } else {
                res.json({ error: 1, msg: '登录失败', data: data })
            }
        }
    })
})



module.exports = myRouter;

